<template>
	<view :style="'height:' + height + 'rpx;position: relative;'">
		<view class="content" style="height: 100%;">
			<view class="logo">
				<u-image width="100%" height="300rpx" :src="src"></u-image>
				<view class="logoText">统一身份认证</view>
			</view>
			<view>
				<u-form :model="form" ref="uForm">
					<u-form-item label="学号" prop="no">
						<u-input v-model="form.no" placeholder="请输入学号" />
					</u-form-item>
					<u-form-item label="密码" prop="password">
						<u-input v-model="form.password" type="password" placeholder="请输入密码" />
					</u-form-item>
				</u-form>
			</view>
			<p class="text">默认密码为'Zhku' + 身份证后六位</p>
			<view class="login">
				<!-- <u-button shape="circle" :custom-style="loginButton" @click="login">登录</u-button> -->
				<u-button shape="circle" :ripple="true" @click="login" type="success">
					<u-icon name="weixin-fill" size="35" class="icon"></u-icon>登录
				</u-button>
			</view>
			<view class="textcss">
				<p class="text">版权所有 © 2021 仲园课程表开发组</p>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad() {
			this.height = getApp().globalData.pageHeight;
		},
		data() {
			return {
				height: 0,
				src: 'http://1.14.247.152:9797/images/17702logo.jpg',
				form: {
					no: "",
					password: "",
					captcha: ""
				},
				loginButton: {
					marginBottom: '30rpx'
				},
				pictureUrl: '',
				isVerification: false,

			}
		},
		props: {
			getinfo: {
				type: Function,
				default: null
			}
		},
		onShow() {
			uni.hideHomeButton()
		},
		//必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			//this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			login() {
				let vm = this
				if (vm.form.no == '' || vm.form.password == '') {
					uni.showModal({
						title: '请输入学号或密码',
						showCancel: false
					})
					return
				}
				if (vm.isVerification == true && vm.form.captcha == '') {
					uni.showModal({
						title: '请输入验证码',
						showCancel: false
					})
					return
				}
				wx.getUserProfile({
					desc: '获取用户基本信息',
					success(res) {
						//console.log(res)
						let body = new Object()
						uni.setStorageSync("userInfo", res.userInfo);
						body.nickName = res.userInfo.nickName
						body.avatarUrl = res.userInfo.avatarUrl
						body.gender = res.userInfo.gender
						body.no = vm.form.no
						body.password = vm.form.password
						wx.login().then(async (res) => {
							body.code = res.code
							uni.showLoading({
								title: '正在登录',
								mask: true
							})
							let data = await getApp().UniRequest("/user/login", "POST", body, "")
							let authority = data.data.Authority
							uni.setStorageSync("Authority", authority);
							let header = new Object()
							header.Authority = authority
							getApp().globalData.header = header
							uni.hideLoading()
							// console.log(data)
							let code = data.code
							if (code == 20000) {
								uni.showToast({
									title: '登录成功!',
									duration: 800
								})
								getApp().globalData.isLogin = true;
								getApp().globalData.newPerson = true;
								getApp().globalData.firstIn = false;
								// console.log('新用户',getApp().globalData.newPerson)
								// getApp().triCountDown()
								// console.log(data)
								uni.switchTab({
									url: '../course/course'
								})
							} else if (code == 20003) {
								uni.showToast({
									title: '账号或密码错误',
									icon: 'error'
								})
							} else if (code == 20007) {
								uni.showToast({
									title: '教务网禁止被访问，请重新登录！',
									icon: 'error'
								})
							}
						})
					}
				})
			},

			// // 确认是否爬完
			// async confirmPa(){
			// 	let vm = this
			// 	let header = new Object()
			// 	header.Authority = uni.getStorageSync("Authority")
			// 	let paRes = await getApp().UniRequest("/authority/info/successPa","Get","",header)
			// 	let result = false;
			// 	console.log(paRes)
			// 	if(paRes.code == 20000){
			// 		//console.log('rr')
			// 		vm.successPa = true
			// 		result = true;
			// 		uni.setStorageSync("successPa",true)
			// 	}
			// 	return result
			// },
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		position: relative;
		margin: 0 50rpx;

		.logo {
			width: 250rpx;
			height: 400rpx;
			margin: 50rpx auto;
			text-align: center;

			.logoText {
				font-size: 40rpx;
				font-weight: bold;
			}
		}

		.text {
			text-align: center;
			color: #828284;
		}

		.icon {
			margin-right: 20rpx;
		}

		.login {
			margin-top: 30rpx
		}

		.authorText {
			.authorTitle {
				font-size: 35rpx;
				margin-bottom: 20rpx;
			}

			._li {
				font-size: 25rpx;
				color: #aaa;
			}

			margin-bottom: 40rpx;
		}

		.flex {
			display: flex;
			justify-content: space-between;

			.input {
				width: 200rpx;
			}

			.captcha {
				width: 350rpx;
			}
		}
	}

	.textcss {
		position: absolute;
		width: 100%;
		bottom: 100rpx;
		text-align: center;
		color: #828284;

		.text {
			width: 100%;
			text-align: center;
		}
	}
</style>
